﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormTimChiTietKhachHang : Form
    {
        private DataContentDataContext db = new DataContentDataContext();
        private FormLapChungTu frmLapChungTu;
//<<<<<<< .mine
//        private FormTimKiemChungTu frmTimChungTu;

//=======
        private FormTimKiemChungTu frmTimKiemChungTu;
//>>>>>>> .r126
        public FormTimChiTietKhachHang()
        {
            InitializeComponent();
        }

        public FormTimChiTietKhachHang(FormLapChungTu frm)
        {
            this.frmLapChungTu = frm;
            InitializeComponent();
            //this.btnTkiemKH.Enabled = true;
            //this.btnAdd.Enabled = false;
            //this.btnThemCTKH.Enabled = false;
            //this.btnhuyCTKh.Enabled = false;
            //this.btnThoatCTKh.Enabled = true;
        }
        public FormTimChiTietKhachHang(FormTimKiemChungTu frm)
        {
            
            InitializeComponent();
            this.frmTimKiemChungTu = frm;
            //this.btnTkiemKH.Enabled = true;
            //this.btnAdd.Enabled = false;
            //this.btnThemCTKH.Enabled = false;
            //this.btnhuyCTKh.Enabled = false;
            //this.btnThoatCTKh.Enabled = true;
        }

        //public FormTimChiTietKhachHang(FormTimKiemChungTu frm)
        //{
        //    this.frmTimChungTu= frm;
        //    InitializeComponent();
        //    //this.btnTkiemKH.Enabled = true;
        //    //this.btnAdd.Enabled = false;
        //    //this.btnThemCTKH.Enabled = false;
        //    //this.btnhuyCTKh.Enabled = false;
        //    //this.btnThoatCTKh.Enabled = true;
        //}

        private void btnhuyCTKh_Click(object sender, EventArgs e)
        {
            this.txtKhachHang.ResetText();
            this.txtKhachHang.Focus();
        }

        private void btnThoatCTKh_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnTkiemKH_Click(object sender, EventArgs e)
        {
            var danhsachkhachhangtk = (from m in db.KHACHHANGs
                                       where m.TenKhachHang.StartsWith(this.txtKhachHang.Text)
                                       select m).ToList();
            this.kHACHHANGBindingSource.DataSource = danhsachkhachhangtk;
           // this.kHACHHANGBindingSource.ResetBindings(true);
            this.dataGridViewDSKH.DataSource = this.kHACHHANGBindingSource;

            //this.btnTkiemKH.Enabled = true;
            //this.btnAdd.Enabled = true;
            //this.btnThemCTKH.Enabled = true;
            //this.btnhuyCTKh.Enabled = true;
            //this.btnThoatCTKh.Enabled = true;
        }

        private void FormTimChiTietKhachHang_Load(object sender, EventArgs e)
        {
            this.dataGridViewDSKH.AutoGenerateColumns = false;

            var chitietkhachhang = (from ctkh in db.KHACHHANGs
                                    orderby ctkh.MaKhachHang ascending
                                    select ctkh
                                        ).ToList();
            this.kHACHHANGBindingSource.DataSource = chitietkhachhang;
        }

        //private void btnAdd_Click(object sender, EventArgs e)
        //{

        //    var khachhang = kHACHHANGBindingSource.Current as KHACHHANG;
        //    if (khachhang != null)
        //    {
        //        if (frmLapChungTu != null)
        //        {
        //            this.frmLapChungTu.comboBoxKH.Text= khachhang.MaKhachHang;
        //            this.DialogResult = DialogResult.OK;
        //            this.Close();
        //        }
        //    }
        //}

        //private void btnThemCTKH_Click(object sender, EventArgs e)
        //{
        //    var kq = new FormChiTietKhachHang(this).ShowDialog();
        //    if (kq == DialogResult.OK)
        //    {
        //        MessageBox.Show("Đã thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        //    }
        //}

        private void btnAdd_Click_1(object sender, EventArgs e)
        {
            var khachhang = this.kHACHHANGBindingSource.Current as KHACHHANG;
            if (khachhang != null)
            {
               // this.frmLapChungTu.txtMaKH.Text = khachhang.MaKhachHang;
                if (frmTimKiemChungTu != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmTimKiemChungTu.comboBoxKH.Text = khachhang.MaKhachHang;
                    this.Close();
                }

                if (frmLapChungTu != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.frmLapChungTu.comboBoxKH.Text = khachhang.MaKhachHang;
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn khách hàng");
            }
        }

        private void btnThemCTKH_Click_1(object sender, EventArgs e)
        {
            var kq = new FormChiTietKhachHang(this).ShowDialog();
            if (kq == DialogResult.OK)
            {
                MessageBox.Show("Đã thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }
}
       
